

ExtGames.FlinchHangoutUtil = function(config) {
	
	ExtGames.FlinchHangoutUtil.superclass.constructor.call(this, config);

	this.myTurn = true;
	this.simPlays = false;
	this.turnIndex = 0;

	this.players = {
		1: 'Matt',
		2: 'Mark',
		3: 'John',
		4: 'Bill'
	};


	this.score1 = 0;
	this.score2 = 0;
	this.score3 = 0;
	this.score4 = 0;

};

Ext.extend(ExtGames.FlinchHangoutUtil, ExtGames.HangoutUtil, {
	
	initialSetup: function(pack) {
		selectionWin.close();
		
		game = new ExtGames.FlinchGame({
    		cards: pack.getCardString()
    	});

		game.newGame();
		game.launch();	
		
		game.nextTurn();
	},
	
	endTurn:function() {
	
		this.myTurn = false;
		
		this.simulateTurns();
	},
	
	nextRound:function() {
		Ext.getCmp('nextRoundButton').enable();
		
		this.myTurn = true;
		game.nextTurn();
	},
	
	submitMayICards:function(id, details) {
		console.log(this.getName(id)+' May I: '+JSON.stringify(details.card));
	},
	
	getOpponentIndex:function(id) {
		return (parseInt(id)-2);
	},
	
	submitMayIRequest:function() {
		
	},
	
	submitNextRound: function() {
		game.reformDeck();
	},
	
	submitScores: function(scores) {
		var opp = this.getOpponents();
		
		var scorecard = {};
		scorecard[this.getMyId()] = scores[0];
		scorecard['2'] = scores[1];
		scorecard['3'] = scores[2];
		scorecard['4'] = scores[3];
			
		game.updateScores(scorecard);
	},
	
	simulateTurns:function() {
		
		this.turnIndex++;
		game.nextTurn();
		
		
		var draw = {};
		draw.id = '2';
		draw.from = 'deck_0';
		draw.card = {
			suit: 'Hearts',
			number: '5'
		};
		game.drewCard(draw);
			
		var c = game.hands[1].items.items[0];
		var discard = {};
		discard.from = 'hand_0';
		discard.to = 'discardPile_0';
		discard.card = {
			suit: c.suit,
			number: c.number
		};		
		//discard.plays = this.simulatePlays(2);
		game.discardedCard(discard);
		
		var c = game.hands[1].items.items[0];
		var reserve = {};
		reserve.to = 'playarea_0';
		reserve.card = {
			suit: c.suit,
			number: c.number
		};
		game.discardedToReserve(reserve);
		
		
		this.turnIndex++;
		game.nextTurn();
		
		/*
		draw = {};
		draw.id = '1';
		draw.from = 'discardpile';
		draw.card = {
			suit: 'Hearts',
			number: '5'
		};
		game.drewCard(draw);
		
		draw = {};
		draw.id = '1';
		draw.from = 'unuseddeck';
		draw.card = {
			suit: 'Hearts',
			number: '5'
		};
		game.drewCard(draw);
		*/
		
		draw = {};
		draw.id = '3';
		draw.from = 'deck_0';
		draw.card = {
			suit: 'Hearts',
			number: '5'
		};
		game.drewCard(draw);
			
		c = game.hands[2].items.items[0];
		discard = {};
		discard.from = 'hand_0';
		discard.to = 'discardPile_0';
		discard.card = {
			suit: c.suit,
			number: c.number
		};
		//discard.plays = this.simulatePlays(3);
		game.discardedCard(discard);
		
		var c = game.hands[2].items.items[0];
		var reserve = {};
		reserve.to = 'playarea_0';
		reserve.card = {
			suit: c.suit,
			number: c.number
		};
		game.discardedToReserve(reserve);
		
		this.turnIndex++;
		game.nextTurn();
		
		
		draw = {};
		draw.id = '4';
		draw.from = 'deck_0';
		draw.card = {
			suit: 'Hearts',
			number: '5'
		};
		game.drewCard(draw);
			
		c = game.hands[3].items.items[0];
		discard = {};
		discard.from = 'hand_0';
		discard.to = 'discardPile_0';
		discard.card = {
			suit: c.suit,
			number: c.number
		};
		//discard.plays = this.simulatePlays(4);
		game.discardedCard(discard);
		
		var c = game.hands[3].items.items[0];
		var reserve = {};
		reserve.to = 'playarea_0';
		reserve.card = {
			suit: c.suit,
			number: c.number
		};
		game.discardedToReserve(reserve);
		
		this.turnIndex = 0;
		this.myTurn = true;
		game.nextTurn();
		
	},
	
	simulatePlays: function(id) {
		
		var p = [];
		
		if (this.simPlays == true) {
			var areas = game.oppplayareas[id-2];
			
			var cards = game.hands[id-2].items.items;
			
			
			p[0] = {
				type: 0,
				pIndex: 0,
				aIndex: 0,
				cards: [{
					suit: cards[1].suit,
					number: cards[1].number
				},{
					suit: cards[2].suit,
					number: cards[2].number
				},{
					suit: cards[3].suit,
					number: cards[3].number
				}]
			};
			
			p[1] = {
					type: 1,
					pIndex: 0,
					aIndex: 1,
					cards: [{
						suit: cards[4].suit,
						number: cards[4].number
					},{
						suit: cards[5].suit,
						number: cards[5].number
					},{
						suit: cards[6].suit,
						number: cards[6].number
					},{
						suit: cards[7].suit,
						number: cards[7].number
					}]
				};
			
		}
		
		return p;
	},
	
	getMyId:function() {
		return '1';
	},
	
	getName: function(id) {
		return this.players[id];
	},
	
	getTurnIndex:function() {
		return this.turnIndex;
	},
	
	getTurnName:function() {
		return 'Matt';
	},
	
	getNumP:function() {
		return 4;
	},
	
	drawCard: function(details) {
		console.log('Drew Card from '+details.from+': '+JSON.stringify(details.card));
	},
	
	discard: function(details) {
		console.log('Discarded: '+JSON.stringify(details));	
	},
	
	discardToReserve: function(details) {
		console.log('Discarded to reserve: '+JSON.stringify(details));	
	},
	
	isTurn:function() {
		return this.myTurn;
	},
	
	getOpponents:function() {
		var opp = [];
		opp[0] = {
			id: '2',
			name: 'Mark'
		};
		opp[1] = {
			id: '3',
			name: 'John'
		};
		opp[2] = {
			id: '4',
			name: 'Bill'
		};
		return opp;
	}


});